package com.sl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ExdeUtil {
	/**
	 * 根据传入时间来获取本周的时间范围
	 * @param a 传进来的时间String类型
	 * @return
	 */
	public static  ArrayList<String> thisWeekDate(){
		/*SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = null;
		try {
			nowDate = sdf.parse(a);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		Calendar cal = Calendar.getInstance();  
	     cal.setTime(nowDate);  
	     // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  
	     int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
	     if (1 == dayWeek) {  
	        cal.add(Calendar.DAY_OF_MONTH, -1);  
	     }  
	     // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期  
	     // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
	     cal.setFirstDayOfWeek(Calendar.MONDAY);  
	     // 获得当前日期是一个星期的第几天  
	     int day = cal.get(Calendar.DAY_OF_WEEK);  
	     // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
	     cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
	     String imptimeBegin = sdf.format(cal.getTime());  
	     // System.out.println("所在周星期一的日期：" + imptimeBegin);  
	     cal.add(Calendar.DATE, 6);  
	     String imptimeEnd = sdf.format(cal.getTime());  
	     // System.out.println("所在周星期日的日期：" + imptimeEnd);  
	     //return imptimeBegin + "," + imptimeEnd;
	     Map<String,String> condition=new HashMap<String,String>();
	     condition.put("beginDate", imptimeBegin);
	     condition.put("endDate", imptimeEnd);
		return condition;*/
		Calendar cal = Calendar.getInstance(); 
	    ArrayList<String> list = new  ArrayList<String>();
	    int date = cal.get(Calendar.DAY_OF_MONTH); 
	    int n = cal.get(Calendar.DAY_OF_WEEK);
	    if (n == 1) { n = 7; } else { n = n - 1; } 
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	    for (int i = 1; i <= 6; i++) {
		    cal.set(Calendar.DAY_OF_MONTH, date + i - n); 
		    list.add(i-1, sdf.format(cal.getTime()));
	    }
	    int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, 6);
	    list.add(sdf.format(cal.getTime()));
	    return  list;
	}
	
	
	public static String datechange(Date date, String pattern) {  
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
        String demo = sdf.format(date);  
        return demo;  
    }
	
	/**
	 * 根据“-”来拆分字符串
	 * @param str
	 * @return
	 */
	public static String splitString (String str){
		String[] s = str.split("-");
		str = "";
		for(int i=0;i<s.length;i++){
			str += s[i];
		}
		return str;
	}
	
	public static String findYearMonth()
	{
		/**
		 * 声明一个int变量year
		 */
		int year;
		/**
		 * 声明一个int变量month
		 */
		int month;
		/**
		 * 声明一个字符串变量date
		 */
		String date;
		/**
		 * 实例化一个对象calendar
		 */
		Calendar calendar = Calendar.getInstance();
		/**
		 * 获取年份
		 */
		year = calendar.get(Calendar.YEAR);
		/**
		 * 获取月份
		 */
		month = calendar.get(Calendar.MONTH) + 1;
		/**
		 * 拼接年份和月份
		 */
		date = year +""+ ( month<10 ? "0" + month : month);
		/**
		 * 返回当前年月
		 */
		return date;
	}
}
